/*  
 *  Pawn.cs 
 *  Copyright(c) 2007 Adam W Adair
 *
 *  This file is part of Adam's Chess Engine (ACE).
 *
 *  ACE is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *   
 *  ACE is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with ACE; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Text;

namespace ACE.Board
{
    /// <summary>
    /// Lowest valued piece of the board.
    /// </summary>
    class Pawn : Piece
    {
        public static readonly int[] MOVEOFFSETS = { };   // Pawn is special.  No offsets.
        public static readonly Piece[] BLACK_PROMOTIONS = {Knight.BlackKnight, Bishop.BlackBishop, Rook.BlackRook, Queen.BlackQueen};
        public static readonly Piece[] WHITE_PROMOTIONS = { Knight.WhiteKnight, Bishop.WhiteBishop, Rook.WhiteRook, Queen.WhiteQueen };
        private static Pawn m_black;
        private static Pawn m_white;
        private const int PAWN_VALUE = 100;

        private Pawn(PlayerColor c)
            : base(c)
        {
            PieceValue = PAWN_VALUE;
        }

        /// <summary>
        /// Gets the black pawn.  There is only one instance of the black pawn
        /// but it gets used repeatedly.
        /// </summary>
        public static Pawn BlackPawn
        {
            get
            {
                if (m_black != null) return m_black;
                else
                {
                    m_black = new Pawn(PlayerColor.Black);
                }
                return m_black;
            }
        }

        /// <summary>
        /// Same with the WhitePawn.  There is only one.
        /// </summary>
        public static Pawn WhitePawn
        {
            get
            {
                if (m_white != null) return m_white;
                else
                {
                    m_white = new Pawn(PlayerColor.White);
                }
                return m_white;
            }
        }

        public override string PieceChar
        {
            get { return "p"; }
        }

        /// <summary>
        /// false.  king does not slide.
        /// </summary>
        public override bool CanSlide
        {
            get { return false; }
        }

        /// <summary>
        /// not used.  The pawn has special ways of moving.
        /// </summary>
        public override int[] MoveOffsets
        {
            get { return MOVEOFFSETS; }
        }

        /// <summary>
        /// pawn has a special way of moving.  
        /// </summary>
        /// <param name="board"></param>
        /// <param name="boardPosition"></param>
        /// <returns></returns>
        public override List<Move> GeneratePotentialMoves(ChessBoard board, int boardPosition)
        {
            List<Move> ml = new List<Move>();
            int from = boardPosition;
            int ep = board.EnpassantSqr;
            if (Color == PlayerColor.White)
            {
                if (ChessBoard.Column(from) != 0 && (board.ContainsEnemy(from - 9, this.Color) || ep == from - 9))
                {
                    int to = from - 9;
                    // check for enpassant
                    if (to == ep)
                        GenerateEnpassantMove(board, from, to, ml);
                    // generate move 
                    else
                        GeneratePawnMove(board, from, to, board.Squares[to].Piece, ml);
                }
                if (ChessBoard.Column(from) != 7 && board.ContainsEnemy(from - 7, this.Color))
                {
                    int to = from - 7;
                    if (to == ep)
                        GenerateEnpassantMove(board, from, to, ml);
                    // generate move 
                    else
                        GeneratePawnMove(board, from, to, board.Squares[to].Piece, ml);
                }
                if (board.Squares[from - 8].Piece == null)
                {
                    //move 1 square ahead
                    GeneratePawnMove(board, from, from - 8, null, ml);
                    if (from >= 48 && board.Squares[from - 16].Piece == null)
                    {
                        //push 2 squares ahead.
                        GeneratePawnMove(board,from , from - 16, null, ml);
                        ml[ml.Count-1].IsPushingPawn2=true;
                    }
                }
            }

            else
            {
                if (ChessBoard.Column(from) != 0 && (board.ContainsEnemy(from + 7, this.Color)))
                {
                    int to = from + 7;
                    if (to == ep)
                        GenerateEnpassantMove(board, from, to, ml);
                    // generate move 
                    else
                        GeneratePawnMove(board, from, to, board.Squares[to].Piece, ml);

                }
                if (ChessBoard.Column(from) != 7 && board.ContainsEnemy(from + 9, this.Color))
                {
                    int to = from + 9;
                    if (to == ep)
                        GenerateEnpassantMove(board, from, to, ml);
                    // generate move 
                    else
                        GeneratePawnMove(board, from, to, board.Squares[to].Piece, ml);
                }
                if (board.Squares[from + 8].Piece == null)
                {
                    // move 1 square ahead
                    GeneratePawnMove(board, from, from + 8, null,ml);
                    if (from <= 15 && board.Squares[from + 16].Piece==null)
                    {
                        //push 2 squares ahead.
                        GeneratePawnMove(board, from, from + 16, null, ml);
                        ml[ml.Count - 1].IsPushingPawn2 = true;
                    }
                }
            }
            return ml;
        }

        /// <summary>
        /// Generates the pawn move
        /// </summary>
        /// <param name="board">board the pawn is moving on</param>
        /// <param name="from">The square the pawn is moving from</param>
        /// <param name="to">The square the pawn is moving to</param>
        /// <param name="capture">Piece the pawn is catureing</param>
        /// <param name="ml">List the move gets added to.</param>
        private void GeneratePawnMove(ChessBoard board, int from, int to, Piece capture, List<Move> ml)
        {
            if ((this.Color == Piece.PlayerColor.White && to <= ChessBoard.H8)||
                (this.Color == Piece.PlayerColor.Black && to >= ChessBoard.A1))
            {
                //Generate the series of promotion moves.
                GeneratePromotionMoves(board, from, to, capture, ml);
                return;
            }
            Move move = new Move(from, to, this);
            move.IsPawnMove = true;
            move.CapturePiece = capture;
            
            ml.Add(move);
        }

        /// <summary>
        /// Generate series of possible promotions for the pawn.
        /// </summary>
        /// <param name="board">The board the pawn is moving on</param>
        /// <param name="from">moving from</param>
        /// <param name="to">moving to</param>
        /// <param name="capture">The piece that is captured (or null if no capture)</param>
        /// <param name="ml">the list the moves get added to.</param>
        private void GeneratePromotionMoves(ChessBoard board, int from, int to, Piece capture, List<Move> ml)
        {
            Move move = null; 
            Piece [] ppieces = null;
            if(this.Color==PlayerColor.White)
            {
                ppieces = WHITE_PROMOTIONS;
            }
            else{
                ppieces = BLACK_PROMOTIONS;
            }
            for(int i = 0; i < ppieces.Length; ++i)
            {
                move = new Move(from, to, this);
                move.IsPromotion = true;
                move.IsPawnMove = true;
                move.PromotionPiece = ppieces[i];
                move.CapturePiece = capture;
                ml.Add(move);
            }
        }

        private void GenerateEnpassantMove(ChessBoard board, int from, int to, List<Move> ml)
        {
            Piece cp = null;
            if (this.Color == PlayerColor.White)
            {
                int captureSquare = to + 8;
                BoardSquare sqr = board.Squares[captureSquare];
                if (sqr.Piece != null && sqr.Piece.Color == PlayerColor.Black)
                {
                    cp = sqr.Piece;
                }

            }
            else
            {
                int captureSquare = to - 8;
                BoardSquare sqr = board.Squares[captureSquare];
                if (sqr.Piece != null && sqr.Piece.Color == PlayerColor.White)
                {
                    cp = sqr.Piece;
                }
            }
            if (cp != null)
            {
                Move tmpMove = new Move(from, to, this);
                tmpMove.IsPawnMove = true;
                tmpMove.IsEnpassantCapture = true;
                tmpMove.CapturePiece = cp;
            }
        }

        /// <summary>
        /// Checks the aquares that a pawn attack.
        /// </summary>
        /// <param name="board">The board to check</param>
        /// <param name="piecePosition">the position of the pawn</param>
        /// <param name="sqaure">The square we are checking</param>
        /// <returns>true if the pawn is attacking the square.</returns>
        public override bool IsAttackingSqaure(ChessBoard board, int piecePosition, int sqaure)
        {
            if(Color==PlayerColor.White)
            {
                if(ChessBoard.Column(piecePosition)!=0 && ((piecePosition-9)==sqaure))
                {
                    return true;
                }
                if(ChessBoard.Column(piecePosition)!=7 && ((piecePosition-7)==sqaure))
                {
                    return true;
                }
            }
            else
            {
                if(ChessBoard.Column(piecePosition)!=0 && ((piecePosition+7)==sqaure))
                {
                    return true;
                }
                if(ChessBoard.Column(piecePosition)!=7 && ((piecePosition+9)==sqaure))
                {
                    return true;
                }
            }
            return false;
        }
    }

}
